//
//  CircleProgressView.swift
//  iOS Example
//
//  Created by xiaoZuDeMeng on 2022/12/2.
//  Copyright © 2022 Kaan Dedeoglu. All rights reserved.
//

import UIKit

class CircleProgressView: UIView {

    let circleLayer: CAShapeLayer = CAShapeLayer()
    
    let progressLayer: CAShapeLayer = CAShapeLayer()
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        circleLayer.frame = self.bounds
        self.layer.addSublayer(circleLayer)
        circleLayer.fillColor = UIColor.clear.cgColor
        circleLayer.strokeColor = UIColor.gray.cgColor
        circleLayer.opacity = 0.2
        circleLayer.lineWidth = 5
        let path: UIBezierPath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: 90, startAngle: CGFloat((M_PI * -210) / 180.0), endAngle: CGFloat((M_PI * 30) / 180.0), clockwise: true)
        circleLayer.path = path.cgPath
        
        progressLayer.frame = bounds
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.strokeColor = UIColor.red.cgColor
        progressLayer.lineCap = .round
        progressLayer.lineWidth = 7
        progressLayer.path = path.cgPath
        progressLayer.strokeEnd = 0.5

        let gradientLayer: CAGradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = [UIColor.orange.cgColor, UIColor.cyan.cgColor]
        gradientLayer.locations = [0, 1]
        gradientLayer.startPoint = CGPoint(x: 0, y: 1)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        gradientLayer.mask = progressLayer
        self.layer.addSublayer(gradientLayer)
    }

    
}
